﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Register : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Visible = false;
    }
    public string mahoa(string pass)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Trim(), "MD5");
    }
    protected void btnInsert_Click1(object sender, EventArgs e)
    {

        var entity = new Customer
        {
            CusId = txtUsername.Text.Trim(),
            Password = (mahoa(txtPassword.Text.Trim())),
            Fullname = txtFullName.Text,
            Email = txtEmail.Text.Trim(),
            Birthday = Convert.ToDateTime(txtBirthday.Text),
            Sex = Convert.ToBoolean(rdo_gioitinh.SelectedValue),
            Phone = txtPhone.Text,
            Address = txtAddress.Text,
            Activated = false
        };
        try
        {
            db.Customers.InsertOnSubmit(entity);
            db.SubmitChanges();
           // Response.Write("ok");
           
            lblMessage.Text = "Đăng ký thành công !";

            String from = "truongnguyen3168@gmail.com";
            String to = txtEmail.Text;
            String subject = "Welcome mail";

            String url = Request.Url.AbsoluteUri.Replace("Register.aspx", "Activate.aspx?id=" + txtUsername.Text);
            String body = "Welcome to website <a href='" + url + "'>Activate</a>";
            Mail.Send(from, to, subject, body);
            Response.Redirect("Dangkythanhcong.aspx");
        }
        catch (Exception ex)
        {
           // register_error.Visible = true;
            if (ex.Message.Contains("PK_Customers"))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Tên đăng nhập đã tồn tại vui lòng chọn tên khác.";
            }
            else
            {
                lblMessage.Text=ex.Message;
            }
             
        }
    }
}